package com.green.cloud.server.business.service;

import com.green.cloud.server.api.HelloService;
import com.green.cloud.server.business.mapper.HelloMapper;
import com.green.cloud.server.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloServiceImpl implements HelloService {

    private HelloMapper helloMapper;

    private StringRedisTemplate stringRedisTemplate;

    private MongoTemplate mongoTemplate;

    @Autowired
    public HelloServiceImpl(HelloMapper helloMapper, StringRedisTemplate stringRedisTemplate, MongoTemplate mongoTemplate) {
        this.helloMapper = helloMapper;
        this.stringRedisTemplate = stringRedisTemplate;
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public User add(@RequestBody User user) {
        helloMapper.add(user);
        return user;
    }

    @Override
    public User user(@RequestParam("id") Long id) {
        return helloMapper.user(id);
    }

    @Override
    public String redisGet(@RequestParam("key") String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public User mongoAdd(@RequestBody User user) {
        return mongoTemplate.save(user, "collection");
    }

    @Override
    public List<User> mongoGet() {
        return mongoTemplate.findAll(User.class, "collection");
    }
}
